
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 并发 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="../part21/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.23" data-path="./">
            
                <a href="./">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 并发</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x5E76;&#x53D1;">Go &#x5E76;&#x53D1;</h1>
<p>Go &#x8BED;&#x8A00;&#x652F;&#x6301;&#x5E76;&#x53D1;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x901A;&#x8FC7; go &#x5173;&#x952E;&#x5B57;&#x6765;&#x5F00;&#x542F; goroutine &#x5373;&#x53EF;&#x3002;</p>
<p>goroutine &#x662F;&#x8F7B;&#x91CF;&#x7EA7;&#x7EBF;&#x7A0B;&#xFF0C;goroutine &#x7684;&#x8C03;&#x5EA6;&#x662F;&#x7531; Golang &#x8FD0;&#x884C;&#x65F6;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x7684;&#x3002;</p>
<p>goroutine &#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">go</span> &#x51FD;&#x6570;&#x540D;( &#x53C2;&#x6570;&#x5217;&#x8868; )
</code></pre>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">go</span> f(x, y, z)
</code></pre>
<p>&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x65B0;&#x7684; <code>goroutine</code>:</p>
<pre><code class="lang-go">f(x, y, z)
</code></pre>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x5141;&#x8BB8;&#x4F7F;&#x7528; go &#x8BED;&#x53E5;&#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8FD0;&#x884C;&#x671F;&#x7EBF;&#x7A0B;&#xFF0C; &#x5373; <code>goroutine</code>&#xFF0C;&#x4EE5;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x3001;&#x65B0;&#x521B;&#x5EFA;&#x7684; <code>goroutine</code> &#x6765;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002; &#x540C;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x4E2D;&#x7684;&#x6240;&#x6709; <code>goroutine</code> &#x5171;&#x4EAB;&#x540C;&#x4E00;&#x4E2A;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#x3002;</p>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
    <span class="hljs-string">&quot;fmt&quot;</span>
    <span class="hljs-string">&quot;time&quot;</span>
)

<span class="hljs-keyword">func</span> say(s <span class="hljs-keyword">string</span>) {
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++ {
        time.Sleep(<span class="hljs-number">100</span> * time.Millisecond)
        fmt.Println(s)
    }
}

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">go</span> say(<span class="hljs-string">&quot;world&quot;</span>)
    say(<span class="hljs-string">&quot;hello&quot;</span>)
}
</code></pre>
<p>&#x6267;&#x884C;&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#xFF0C;&#x4F60;&#x4F1A;&#x770B;&#x5230;&#x8F93;&#x51FA;&#x7684; hello &#x548C; world &#x662F;&#x6CA1;&#x6709;&#x56FA;&#x5B9A;&#x5148;&#x540E;&#x987A;&#x5E8F;&#x3002;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x662F;&#x4E24;&#x4E2A; goroutine &#x5728;&#x6267;&#x884C;&#xFF1A;</p>
<pre><code class="lang-sh">world
hello
hello
world
world
hello
world
hello
hello
world
</code></pre>
<hr>
<h2 id="&#x901A;&#x9053;&#xFF08;channel&#xFF09;">&#x901A;&#x9053;&#xFF08;channel&#xFF09;</h2>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x901A;&#x9053;&#xFF08;channel&#xFF09;&#x662F;&#x7528;&#x6765;&#x4F20;&#x9012;&#x6570;&#x636E;&#x7684;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x901A;&#x9053;&#x53EF;&#x7528;&#x4E8E;&#x4E24;&#x4E2A; <code>goroutine</code> &#x4E4B;&#x95F4;&#x901A;&#x8FC7;&#x4F20;&#x9012;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x6765;&#x540C;&#x6B65;&#x8FD0;&#x884C;&#x548C;&#x901A;&#x8BAF;&#x3002;&#x64CD;&#x4F5C;&#x7B26; <code>&lt;-</code> &#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x901A;&#x9053;&#x7684;&#x65B9;&#x5411;&#xFF0C;&#x53D1;&#x9001;&#x6216;&#x63A5;&#x6536;&#x3002;&#x5982;&#x679C;&#x672A;&#x6307;&#x5B9A;&#x65B9;&#x5411;&#xFF0C;&#x5219;&#x4E3A;&#x53CC;&#x5411;&#x901A;&#x9053;&#x3002;</p>
<pre><code class="lang-go">ch &lt;- v    <span class="hljs-comment">// &#x628A; v &#x53D1;&#x9001;&#x5230;&#x901A;&#x9053; ch</span>
v := &lt;- ch  <span class="hljs-comment">// &#x4ECE; ch &#x63A5;&#x6536;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x628A;&#x503C;&#x8D4B;&#x7ED9; v</span>
</code></pre>
<p>&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x901A;&#x9053;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;chan&#x5173;&#x952E;&#x5B57;&#x5373;&#x53EF;&#xFF0C;&#x901A;&#x9053;&#x5728;&#x4F7F;&#x7528;&#x524D;&#x5FC5;&#x987B;&#x5148;&#x521B;&#x5EFA;&#xFF1A;</p>
<pre><code class="lang-go">ch := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>)
</code></pre>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong> &#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x901A;&#x9053;&#x662F;&#x4E0D;&#x5E26;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x3002;&#x53D1;&#x9001;&#x7AEF;&#x53D1;&#x9001;&#x6570;&#x636E;&#xFF0C;&#x540C;&#x65F6;&#x5FC5;&#x987B;&#x6709;&#x63A5;&#x6536;&#x7AEF;&#x76F8;&#x5E94;&#x7684;&#x63A5;&#x6536;&#x6570;&#x636E;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x901A;&#x8FC7;&#x4E24;&#x4E2A; goroutine &#x6765;&#x8BA1;&#x7B97;&#x6570;&#x5B57;&#x4E4B;&#x548C;&#xFF0C;&#x5728; goroutine &#x5B8C;&#x6210;&#x8BA1;&#x7B97;&#x540E;&#xFF0C;&#x5B83;&#x4F1A;&#x8BA1;&#x7B97;&#x4E24;&#x4E2A;&#x7ED3;&#x679C;&#x7684;&#x548C;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> sum(s []<span class="hljs-keyword">int</span>, c <span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>) {
    sum := <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> _, v := <span class="hljs-keyword">range</span> s {
        sum += v
    }
    c &lt;- sum    <span class="hljs-comment">// &#x628A; sum &#x53D1;&#x9001;&#x5230;&#x901A;&#x9053; c</span>
}

<span class="hljs-keyword">func</span> main() {
    s := [] <span class="hljs-keyword">int</span> {<span class="hljs-number">7</span>, <span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-number">-9</span>, <span class="hljs-number">4</span>, <span class="hljs-number">0</span>}
    c := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>)
    <span class="hljs-keyword">go</span> sum(s[:<span class="hljs-built_in">len</span>(s) / <span class="hljs-number">2</span>], c)
    <span class="hljs-keyword">go</span> sum(s[<span class="hljs-built_in">len</span>(s) / <span class="hljs-number">2</span>:], c)
    x, y := &lt;- c, &lt;- c        <span class="hljs-comment">// &#x4ECE;&#x901A;&#x9053; c &#x4E2D;&#x63A5;&#x6536;</span>
    fmt.Println(x, y, x + y)
}
</code></pre>
<p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">-5 17 12
</code></pre>
<h3 id="&#x901A;&#x9053;&#x7F13;&#x51B2;&#x533A;">&#x901A;&#x9053;&#x7F13;&#x51B2;&#x533A;</h3>
<p>&#x901A;&#x9053;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x7F13;&#x51B2;&#x533A;&#xFF0C;&#x901A;&#x8FC7; make &#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x6307;&#x5B9A;&#x7F13;&#x51B2;&#x533A;&#x5927;&#x5C0F;&#xFF1A;</p>
<pre><code class="lang-go">ch := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>, <span class="hljs-number">100</span>)
</code></pre>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x5E26;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x901A;&#x9053;&#x5141;&#x8BB8;&#x53D1;&#x9001;&#x7AEF;&#x7684;&#x6570;&#x636E;&#x53D1;&#x9001;&#x548C;&#x63A5;&#x6536;&#x7AEF;&#x7684;&#x6570;&#x636E;&#x83B7;&#x53D6;&#x5904;&#x4E8E;&#x5F02;&#x6B65;&#x72B6;&#x6001;&#xFF0C;&#x5C31;&#x662F;&#x8BF4;&#x53D1;&#x9001;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x53EF;&#x4EE5;&#x653E;&#x5728;&#x7F13;&#x51B2;&#x533A;&#x91CC;&#x9762;&#xFF0C;&#x53EF;&#x4EE5;&#x7B49;&#x5F85;&#x63A5;&#x6536;&#x7AEF;&#x53BB;&#x83B7;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7ACB;&#x523B;&#x9700;&#x8981;&#x63A5;&#x6536;&#x7AEF;&#x53BB;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x4E0D;&#x8FC7;&#x7531;&#x4E8E;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x5927;&#x5C0F;&#x662F;&#x6709;&#x9650;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x8FD8;&#x662F;&#x5FC5;&#x987B;&#x6709;&#x63A5;&#x6536;&#x7AEF;&#x6765;&#x63A5;&#x6536;&#x6570;&#x636E;&#x7684;&#xFF0C;&#x5426;&#x5219;&#x7F13;&#x51B2;&#x533A;&#x4E00;&#x6EE1;&#xFF0C;&#x6570;&#x636E;&#x53D1;&#x9001;&#x7AEF;&#x5C31;&#x65E0;&#x6CD5;&#x518D;&#x53D1;&#x9001;&#x6570;&#x636E;&#x4E86;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong> &#x5982;&#x679C;&#x901A;&#x9053;&#x4E0D;&#x5E26;&#x7F13;&#x51B2;&#xFF0C;&#x53D1;&#x9001;&#x65B9;&#x4F1A;&#x963B;&#x585E;&#x76F4;&#x5230;&#x63A5;&#x6536;&#x65B9;&#x4ECE;&#x901A;&#x9053;&#x4E2D;&#x63A5;&#x6536;&#x4E86;&#x503C;&#x3002;&#x5982;&#x679C;&#x901A;&#x9053;&#x5E26;&#x7F13;&#x51B2;&#xFF0C;&#x53D1;&#x9001;&#x65B9;&#x5219;&#x4F1A;&#x963B;&#x585E;&#x76F4;&#x5230;&#x53D1;&#x9001;&#x7684;&#x503C;&#x88AB;&#x62F7;&#x8D1D;&#x5230;&#x7F13;&#x51B2;&#x533A;&#x5185;&#xFF1B;&#x5982;&#x679C;&#x7F13;&#x51B2;&#x533A;&#x5DF2;&#x6EE1;&#xFF0C;&#x5219;&#x610F;&#x5473;&#x7740;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x76F4;&#x5230;&#x67D0;&#x4E2A;&#x63A5;&#x6536;&#x65B9;&#x83B7;&#x53D6;&#x5230;&#x4E00;&#x4E2A;&#x503C;&#x3002;&#x63A5;&#x6536;&#x65B9;&#x5728;&#x6709;&#x503C;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x4E4B;&#x524D;&#x4F1A;&#x4E00;&#x76F4;&#x963B;&#x585E;&#x3002;</p>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x5B58;&#x50A8;&#x6574;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x5E26;&#x7F13;&#x51B2;&#x901A;&#x9053;</span>
    <span class="hljs-comment">// &#x7F13;&#x51B2;&#x533A;&#x5927;&#x5C0F;&#x4E3A;2</span>
    ch := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>, <span class="hljs-number">2</span>)

    <span class="hljs-comment">// &#x56E0;&#x4E3A; ch &#x662F;&#x5E26;&#x7F13;&#x51B2;&#x7684;&#x901A;&#x9053;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x53D1;&#x9001;&#x4E24;&#x4E2A;&#x6570;&#x636E;</span>
    <span class="hljs-comment">// &#x800C;&#x4E0D;&#x7528;&#x7ACB;&#x523B;&#x9700;&#x8981;&#x53BB;&#x540C;&#x6B65;&#x8BFB;&#x53D6;&#x6570;&#x636E;</span>
    ch &lt;- <span class="hljs-number">1</span>
    ch &lt;- <span class="hljs-number">2</span>

    <span class="hljs-comment">// &#x83B7;&#x53D6;&#x8FD9;&#x4E24;&#x4E2A;&#x6570;&#x636E;</span>
    fmt.Println(&lt;- ch)
    fmt.Println(&lt;- ch)
}
</code></pre>
<p>&#x6267;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">1
2
</code></pre>
<h3 id="go-&#x904D;&#x5386;&#x901A;&#x9053;&#x4E0E;&#x5173;&#x95ED;&#x901A;&#x9053;">Go &#x904D;&#x5386;&#x901A;&#x9053;&#x4E0E;&#x5173;&#x95ED;&#x901A;&#x9053;</h3>
<p>Go &#x901A;&#x8FC7; range &#x5173;&#x952E;&#x5B57;&#x6765;&#x5B9E;&#x73B0;&#x904D;&#x5386;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x4E0E;&#x6570;&#x7EC4;&#x6216;&#x5207;&#x7247;&#x3002;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go">v, ok := &lt;- ch
</code></pre>
<p>&#x5982;&#x679C;&#x901A;&#x9053;&#x63A5;&#x6536;&#x4E0D;&#x5230;&#x6570;&#x636E;&#x540E; ok &#x5C31;&#x4E3A; false&#xFF0C;&#x8FD9;&#x65F6;&#x901A;&#x9053;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <strong><code>close()</code></strong> &#x51FD;&#x6570;&#x6765;&#x5173;&#x95ED;&#x3002;</p>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> fibonacci(n <span class="hljs-keyword">int</span>, c <span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>) {
    x, y := <span class="hljs-number">0</span>, <span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; n; i++ {
        c &lt;- x
        x, y = y, x+y
    }
    <span class="hljs-built_in">close</span>(c)
}

<span class="hljs-keyword">func</span> main() {
    c := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>, <span class="hljs-number">10</span>)
    <span class="hljs-keyword">go</span> fibonacci(<span class="hljs-built_in">cap</span>(c), c)
    <span class="hljs-comment">/* range &#x51FD;&#x6570;&#x904D;&#x5386;&#x6BCF;&#x4E2A;&#x4ECE;&#x901A;&#x9053;&#x63A5;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x56E0;&#x4E3A; c &#x5728;&#x53D1;&#x9001;&#x5B8C; 10 &#x4E2A;
    &#x6570;&#x636E;&#x4E4B;&#x540E;&#x5C31;&#x5173;&#x95ED;&#x4E86;&#x901A;&#x9053;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x6211;&#x4EEC; range &#x51FD;&#x6570;&#x5728;&#x63A5;&#x6536;&#x5230; 10 &#x4E2A;&#x6570;&#x636E;
    &#x4E4B;&#x540E;&#x5C31;&#x7ED3;&#x675F;&#x4E86;&#x3002;&#x5982;&#x679C;&#x4E0A;&#x9762;&#x7684; c &#x901A;&#x9053;&#x4E0D;&#x5173;&#x95ED;&#xFF0C;&#x90A3;&#x4E48; range &#x51FD;&#x6570;&#x5C31;&#x4E0D;
    &#x4F1A;&#x7ED3;&#x675F;&#xFF0C;&#x4ECE;&#x800C;&#x5728;&#x63A5;&#x6536;&#x7B2C; 11 &#x4E2A;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x963B;&#x585E;&#x4E86;&#x3002;*/</span>
    <span class="hljs-keyword">for</span> i := <span class="hljs-keyword">range</span> c {
        fmt.Println(i)
    }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part21/" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Go 错误处理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 并发","level":"1.23","depth":1,"previous":{"title":"Go 错误处理","level":"1.22","depth":1,"path":"part21/README.md","ref":"part21/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part22/README.md","mtime":"2021-01-20T11:14:21.427Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

